﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using MobileShop.Controls;

namespace MobileShop
{
    public partial class Search : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lstCategory.Rebind();
                PageMap = "";
                if (CategoryID > 0)
                {
                    View_CategoryTree c = new View_CategoryTree();
                    c.Where.ID.Value = CategoryID;
                    c.Query.Load();
                    string str = "";
                    string[] pathNames = c.s_Path.Split(new string[] { " - " }, StringSplitOptions.RemoveEmptyEntries);
                    string[] pathIDs = c.s_IDPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
                    for (int i = 0; i < pathNames.Length; i++)
                    {
                        str += string.Format("{1}[{0}]", this.ResolveUrl(string.Format("~/Search.aspx?category={0}_{1}", Utilities.GetValidParam(pathNames[i]), pathIDs[i])),pathNames[i]);
                        if (i < pathNames.Length - 1)
                            str += "|";
                    }

                    PageMap = string.Format("Product[{0}]|{1}", this.ResolveUrl("~/Search.aspx?category=All_0"), str);
                }
                else
                {
                    PageMap = string.Format("Product[{0}]|", this.ResolveUrl("~/Search.aspx?category=All_0"));
                }
            }
        }

        protected int CategoryID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["category"]))
                {
                    return MobileShop.Utilities.GetParmValue(Request.QueryString["category"]);
                }
                return -1;
            }
        }

        protected void lstCategory_OnNeedDataSource(object sender, EventArgs e)
        {
            Categories cats = null;
            if (CategoryID > 0)
            {
                cats = Categories.GetAllWebActiveCategoriesByParent(CategoryID);    
            }
            else if (CategoryID == 0)
            {
                cats = Categories.GetAllWebActiveCategoriesByParent(null);
            }

            if (cats != null && cats.RowCount > 0)
                lstCategory.DataSource = cats.DefaultView;
            else
                Response.Redirect(this.ResolveUrl("~/SearchPrd.aspx") + Request.Url.Query);
        }
    }
}